package com.cicadalane.androlate.popup.actions;

/*
 * Copyright (C) 2011 cicada.software@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.internal.resources.Project;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import com.cicadalane.androlate.AndrolateWizard;
import com.cicadalane.androlate.Android;
import com.cicadalane.androlate.AndrolateWizardDialog;

public class TranslateAction implements IObjectActionDelegate {
	private Shell shell;
	private ISelection mSelection = null;
	private IWorkbench mWorkbench = null;

	/**
	 * Constructor for Action1.
	 */
	public TranslateAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
		mWorkbench = targetPart.getSite().getWorkbenchWindow().getWorkbench();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		IStructuredSelection selectionToPass = StructuredSelection.EMPTY;
		if (mSelection instanceof IStructuredSelection) {
			selectionToPass = (IStructuredSelection) mSelection;
		} else {
			MessageDialog
					.openInformation(shell, "Error",
							"Unable to continue, please ensure you have a project selected.");
			return;
		}

		if (selectionToPass.size() != 1)
			return;

		Object element = selectionToPass.getFirstElement();
		if (!(element instanceof IProject))
			return;

		IProject project = (IProject) element;
		IFile manifestFile = Android.getManifest(project);
		if (manifestFile == null) {
			MessageDialog
					.openInformation(
							shell,
							"Error",
							Android.MANIFEST_FILENAME
									+ " file not found, please select an Android project.");
			return;
		}

		// Create the wizard
		AndrolateWizard wizard = new AndrolateWizard(project);

		// Create the wizard dialog
		AndrolateWizardDialog dialog = new AndrolateWizardDialog(shell, wizard);

		dialog.create();
		dialog.open();
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		mSelection = selection;
	}
}
